pick random string from array javascript

49

pick a random element from an array javascript -

var myArray = [
  "Apples",
  "Bananas",
  "Pears"
];

var randomItem = myArray[Math.floor(Math.random()*myArray.length)];

javascript how to get a random element from an array -

var items = ['Yes', 'No', 'Maybe'];
var item = items[Math.floor(Math.random() * items.length)];

js random string from array -

const randomElement = array[Math.floor(Math.random() * array.length)];

javascript generate random string from array -

Math.random().toString(36).substr(2, 5);

Comments

Submit
0 Comments